#!/bin/guile \
-e main -s
!#

(define (main args)
  ;;(display (get-type args))
  (let loop ((args args))
    (unless (null? (cdr args))
      (let ((num (string->number (car (cdr args))))
            (den (string->number (car (cdr (cdr args))))))
        ;; (display (car (cdr args)))
        ;; (display " ")
        ;; (display (car (cdr (cdr args))))
        ;; (display "\n")
        (simplyify num den)
        (loop (cdr (cdr args)))))))

(define (simplyify num den)
  (let loop ((factor (commonFactor num den)))
    (if (number? factor)
        (begin
          (set! num (/ num factor))
          (set! den (/ den factor))
          (loop (commonFactor num den)))
        ;;(cons num (cons den '()))
        (begin
          (display num)
          (display " ")
          (display den)
          (display "\n")))))

(define (commonFactor a b)
  ;;the named loop defined a closure.  It's essentially naming a function called loop, and calling it repeatily
  (let loop ((factor 2))
    ;; if facter is half of the number, then return #f
    (cond ((= factor (1+ (ceiling (/ a 2))))
           ;; if factor is half of a, then a is prime.
           ;; test to see if a is a factor of b, and a != b
           ;; Also a != 1... I feel like this is not very mathematical
           (if (and (not (= a b)) (= 0 (modulo b a)) (not (= a 1)))
               a
               #f))
          ((= 0 (modulo a factor) (modulo b factor))
           factor)
          ;;if factor does not evenly divide a and b, then start the loop again
          (else (loop (1+ factor))))))
